if(!window.console){
    window.console = {};
}

var server=ctx;//页面服务地址
var apiroot=server;//api服务接口地址
var smslimit=60;
var loginoutlimit=30;//30分钟登录超时时间
var loginCookiename="_loginstate_";
var loginUrl=server+"/we/user/login";
var errorUrl=server+"/we/message";

var $p=new Object();
$p.openWins=new Array();
$p.nextWinNum=0;
$p.winReturnValue=null;
var $w={
	httpRequest:function(opts){
        var m=	opts.method || 'post';
		$.ajax({
			type: m,
			url:opts.url,
			data:opts.data,
			dataType:"json",
			success: function(ret){

				if(opts.all)opts.all(ret);
                if(ret.MsgCode=='0'){window.location.href=errorUrl;}
				else if(ret.MsgCode=='1'){if(opts.ok)opts.ok(ret);}
				else if(ret.MsgCode=='-1'){
					//未登录或登录超时
					var exec=true;
					if(opts.nologin){var funre=opts.nologin(ret);if(funre!=undefined)exec=funre;}
					if(exec){
						sessionStorage.setItem("_curUrl_",window.location.href);
						window.location.href=loginUrl;
					}
				}
				else{
					var exec=true;
					if(opts.fail){var funre=opts.fail(ret);if(funre!=undefined)exec=funre;}
					// if(exec)alert(ret.MsgDesc);
				}
			},
			error: function(xhr, ajaxOptions, thrownError){
				//xhr.responseText
				if(opts.all)opts.all(xhr);
				if(opts.off)opts.off(xhr);
                window.location.href=errorUrl;
			}
		});
	},loadPanel:function(url,data,targetid){
		$.ajax({
			url:url,
			data:data,
			dataType:"html",
			success:function(ret){
				$("#"+targetid).html(ret);
			}
		})
	},checkLogin:function(tologin,tourl){
		var user=$w.getLoginUser();
		if(user!=null)return true;
		else{
			if(tologin)showloginpage(tourl);
			return false;
		}
	},checkAuth:function(tourl,fun){
		$w.httpRequest({
			url:apiroot+"/user/checkauth",
			all:function(ret){
				if(ret.Data=='nologin')showloginpage(tourl);
				else if(ret.Data=='noauth')window.location.href=ctx+'/user/certification';
				else fun(ret);
			}
		})
	},setLoginUser:function(uid,uname,phone){
		var user=new Object();
		user.uid=uid;
		user.uname=uname;
		user.phone=phone;
		user.lasttime=new Date().getTime();
		setCookie(loginCookiename,JSON.stringify(user));
		//sessionStorage.setItem(loginCookiename,JSON.stringify(user));
	},getLoginUser:function(){
		//var s=sessionStorage.getItem(loginCookiename);
		var s=getCookie(loginCookiename);
		if(s){
			if(s!=null){
				var u=JSON.parse(s);
				var m=Number(u.lasttime);
				var nowt=new Date().getTime();
				var diff=nowt-m;
				if(diff<=(loginoutlimit*1000*60))return u;
			}
		}
		return null;
	},clearLoginUser:function(){
		//sessionStorage.removeItem(loginCookiename);
		//delCookie(loginCookiename);
		// 	var user=$w.getLoginUser();
		// 	if(user!=null){
		// 		var nowt=new Date().getTime()-(24 * 60 * 60 * 1000);
		// 		user.lasttime=nowt;
		// 		setCookie(loginCookiename,JSON.stringify(user));
		// 	}
	},upLoginCookieTime:function(u){
		var s=getCookie(loginCookiename);
		if(s){
			if(s!=null){
				var user=JSON.parse(s);
				var nowt=new Date().getTime();
				user.lasttime=nowt;
				setCookie(loginCookiename,JSON.stringify(user));
			}
		}
	},isPhone:function(str){
		var igMobile=/^(?:13\d|15\d|18\d)\d{5}(\d{3}|\*{3})$/;   
		var igPhone=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
		var bl=igMobile.test(str);
		if(!bl)bl=igPhone.test(str);
		return bl;
	},isMobile:function(str){
		var igMobile=/^(?:13\d|15\d|18\d|17\d)\d{5}(\d{3}|\*{3})$/; 
		var bl=igMobile.test(str);
		return bl;
	},
	openWin:function(ops){
		$w.setWinReturn(null);
		var wid="";
		if(ops.id)
			wid=ops.id;
		else{
			$p.nextWinNum=$p.nextWinNum+1;
			wid="_MailWinDiv"+$p.nextWinNum;
		}
		var bl=false;
		for(var i=0;i<$p.openWins.length;i++) 
	  {
	  	if($p.openWins[i]==wid){
	  		bl=true;
	  		break;
	  	}
	  }
	  
		if(!bl){
			$p.openWins.add(wid);
			var ic="icon_win";
			var doAfterClose;
			if(ops.afterClose)doAfterClose=ops.afterClose;
			else{
				doAfterClose=function(re){}
			}
			if(ops.icon)ic=ops.icon;
			var closefun=function(){$p.openWins.remove(wid);$("iframe",$('#'+wid)).attr("src","about:blank");if(navigator.userAgent.indexOf("MSIE")>0) {CollectGarbage();}$('#'+wid).window('destroy');$('#'+wid).remove();doAfterClose($w.getWinReturn());};
			$("<div id='"+wid+"'></div>").appendTo(document.body);
			var wintitle=ops.title || '新窗口';
			if(ops.iframe){
				var h=ops.h;
				if(ops.maximized)
					 h=$(window).height();
				$('#'+wid).window({
					title:wintitle,
			    width:ops.w,
			    height:ops.h,
			    modal:ops.modal,
			    inline:true,
			    collapsible:ops.collapsible,
			    minimizable:ops.minimizable,
			    maximizable:ops.maximizable,
			    resizable:ops.resizable,
			    maximized:ops.maximized,
			    content:'<iframe id="'+wid+'_Iframe" name="'+wid+'_Iframe" src="'+ops.url+'" width="100%" height="'+(h-45)+'" frameborder="0" scrolling="no"></iframe>',
			    onClose:closefun
				});
				var st=$(window).scrollTop();
				var h=($(window).height()-ops.h)/2;
				h=h+st;
				$('#'+wid).window("move",{top:h+'px'})
			}else{
				if(ops.url){
					$('#'+wid).window({
						title:wintitle,
				    width:ops.w,
				    height:ops.h,
				    modal:ops.modal,
				    collapsible:ops.collapsible,
				    minimizable:ops.minimizable,
				    maximizable:ops.maximizable,
				    resizable:ops.resizable,
				    maximized:ops.maximized,
				    onClose:closefun
					});
					var st=$(window).scrollTop();
					var h=($(window).height()-ops.h)/2;
					h=h+st;
					$('#'+wid).window("move",{top:h+'px'})
					$('#'+wid).window("refresh",ops.url);
				}else if(ops.html){
					$('#'+wid).window({
						title:wintitle,
				    width:ops.w,
				    height:ops.h,
				    modal:ops.modal,
				    collapsible:ops.collapsible,
				    minimizable:ops.minimizable,
				    maximizable:ops.maximizable,
				    resizable:ops.resizable,
				    maximized:ops.maximized,
				    content:ops.html,
				    onClose:closefun
					});
					var st=$(window).scrollTop();
					var h=($(window).height()-ops.h)/2;
					h=h+st;
					$('#'+wid).window("move",{top:h+'px'})
				}
			}
			
		}else{
			if(ops.fresh){
					if(ops.iframe)$("iframe",$('#'+wid)).attr("src",ops.url);
					else $('#'+wid).window("refresh",ops.url);
			}
			else $('#'+wid).window("open");
		}	
	},setWinReturn:function(returnvalue){
		$p.winReturnValue=returnvalue;
	},
	getWinReturn:function(){
		return $p.winReturnValue;
	},
	closeCurrWin:function(returnvalue){
		if(returnvalue)
			$w.setWinReturn(returnvalue);
		this.currWin().window('close');
	},
	currWin:function(){
		var cw;
		var czi=0;
		for(i=0;i<$p.openWins.length;i++){
			var zindex=parseInt($("#"+$p.openWins[i]).window("panel").css("zIndex"));
			if(zindex>czi){
				czi=zindex;
				cw=$("#"+$p.openWins[i]);
			}
		}
		return cw;
	}
}

$p.openWins.add=function(wid){
	$p.openWins[$p.openWins.length]=wid;
}

$p.openWins.remove=function(wid) 
{ 
    for(var i=0;i<this.length;i++) 
    { 
        if(this[i]==wid){
        	
        	for(var j=(i+1);j<this.length;j++){
        		this[j-1]=this[j];
        		
        	}
        	break
        }
    }
    if(this.length>=1)
    this.length-=1;
}

//输入法表情编码
function utf16toEntities(str) {
    var patt=/[\ud800-\udbff][\udc00-\udfff]/g;
    // 检测utf16字符正则
    str = str.replace(patt, function(char){
        var H, L, code;
        if (char.length===2) {
            H = char.charCodeAt(0);
            // 取出高位
            L = char.charCodeAt(1);
            // 取出低位
            code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00;
            // 转换算法
            return "&#" + code + ";";
        } else {
            return char;
        }
    });
    return str;
}
//输入法表情解码
function entitiestoUtf16(str){
    // 检测出形如&#12345;形式的字符串
    var strObj=utf16toEntities(str);
    var patt = /&#\d+;/g;
    var H,L,code;
    var arr = strObj.match(patt)||[];
    for (var i=0;i<arr.length;i++){
        code = arr[i];
        code=code.replace('&#','').replace(';','');
        // 高位
        H = Math.floor((code-0x10000) / 0x400)+0xD800;
        // 低位
        L = (code - 0x10000) % 0x400 + 0xDC00;
        code = "&#"+code+";";
        var s = String.fromCharCode(H,L);
        strObj.replace(code,s);
    }
    return strObj;
}

function setCookie(name,value)
{
	var exp = new Date();
	exp.setTime(exp.getTime() + 365*24*60*60*1000);
	document.cookie = name + "="+ escape (value) + ";path=/;expires=" + exp.toGMTString();
}
function getCookie(name)
{
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg))
		return unescape(arr[2]);
	else
		return null;
}
function delCookie(name)
{
	var exp = new Date();
	exp.setTime(exp.getTime() + (-1 * 24 * 60 * 60 * 1000));
	var cval=getCookie(name);
	if(cval!=null)
		document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
function inputMonery(obj){
	$(obj).val($(obj).val().replace(/[^0-9.]/g,''));
}
function inputNumber(obj){
	$(obj).val($(obj).val().replace(/[^0-9]/g,''));
}
function inputAmount(obj){
	$(obj).val($(obj).val().replace(/[^0-9]/g,''));
	if($(obj).val()=='0' || $(obj).val()=='')$(obj).val('1');
	if($(obj).attr("maxnum") && $(obj).val()){
		var max=parseInt($(obj).attr("maxnum"));
		if(parseInt($(obj).val())>max)$(obj).val(max);
	}
}
function IsDate(mystring) {
	var reg = /^(\d{4})-(\d{2})-(\d{2})$/;
	var str = mystring;
	var arr = reg.exec(str);
	if (str=="") return true;
	if (reg.test(str)&&RegExp.$2<=12&&RegExp.$3<=31){
		return true;
	}else
		return false;
}

(function($){
 $.fn.uTable = function(args){
	
	return this.each(function() {
		
	});
};
})(jQuery);

function showInformation(id,type,url){
	if(type==2)window.open(url);
	else if(type==3)window.open(ctx+"/info/info?id="+id);
}
function setBrowsepriceid(priceid,clscode){
	setCookie("browsepriceid",priceid);
	if(clscode)setCookie("browseclscode",clscode);
}
function getBrowsepriceid(){
	var priceid=getCookie("browsepriceid");
	if(!priceid)priceid="";
	return priceid;
}
function getBrowsclscode(){
	var clscode=getCookie("browseclscode");
	if(!clscode)clscode="";
	return clscode;
}
function getQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}
$w.upLoginCookieTime();

$(function(){
	$("input.focus-auto-select").focus(function(e){
		$(this).select();
	});
});